<template>
  <view class="viewport">
    <view class="logo">
      <image src="@/static/img/rentiplogo.png"></image>
    </view>
    <view class="login">
      <button
        class="butttonsubmit"
        open-type="getPhoneNumber"
        @getphonenumber="onGetphonenumber"
      >
        本机号码一键登录
      </button>
    </view>
  </view>
  <!-- <view class="" style="margin-top: 50px" v-if="false">
    <view @click="getUserInfo()">点击登录</view>
    <button
      class="button phone"
      open-type="getPhoneNumber"
      @getphonenumber="onGetphonenumber"
    >
      手机号快捷登录
    </button>
  </view> -->
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { onLoad } from "@dcloudio/uni-app";
// 做手机号绑定会员
import { login, membersinfo } from "@/utils/api";
// #ifdef MP-WEIXIN
// 获取 code 登录凭证
let code = "";
let bizId = "";
onLoad(async options => {
	if (options.bizId) {
		bizId = options.bizId
	}
  const res = await wx.login();
  code = res.code;
});
// 获取手机号码
const onGetphonenumber = (ev) => {
  let param = {
    phoneCode: ev.detail.code,
    code: code,
		manageUserId: bizId,
    grant_type: "wx_mini_app",
  };
  login(param).then((res) => {
    uni.showToast({ icon: "success", title: "登录成功" });
    uni.setStorageSync("token_type", res.data.token_type);
    uni.setStorageSync("token", res.data.access_token);

		membersinfo().then((meinfo) => {
		  if (meinfo) {
		    uni.setStorageSync("userinfo", JSON.stringify(meinfo.data));
				uni.navigateBack()
		  }
		});
  });
};

const getUserInfo = () => {
  var that = this;
  uni.showLoading({
    // 展示加载框
    title: "加载中",
  });

  // 调用 action ，请求登录接口
  uni.login({
    provider: "weixin",
    onlyAuthorize: true, // 微信登录仅请求授权认证
    success: (res) => {
      that.code = res.code;

      //请求登录接口
      if (res.errMsg == "login:ok") {
        var params = {
          code: that.code,
          nickname: that.nickName,
          avatar: that.avatarUrl,
        };
        that.$api.appPlateForm(
          "POST",
          "auth/login",
          params,
          function (res) {
            if (res.code == 200) {
              //获取到token 存入缓存。通过有无token来判断是否登录
              uni.setStorageSync("localtoken", res.data.data.access_token);

              let pages = getCurrentPages(); //获取所有页面的数组对象
              let currPage = pages[pages.length - 1]; //当前页面
              currPage.onLoad(currPage.options); // 传入参数刷新当前页面
              currPage.onShow(); // 传入参数刷新当前页面

              that.myProfile(); //用户信息接口

              uni.showToast({
                title: "登录成功",
                icon: "success",
                mask: true,
              });
            }
          },
          function (err) {
            uni.showToast({
              icon: "none",
              title: err.msg,
            });
          }
        );
      }
    },
  });
};
</script>
<style>
page {
  background: #ffffff;
}
</style>
<style lang="scss" scoped>
.viewport {
  display: flex;
  flex-direction: column;
  height: 50%;
  padding: 20rpx 40rpx;
  background: url("http://f.sheshouduo.cn/x/s1/loginbgtop.png")
    no-repeat;
  background-size: 100% 650rpx;
}

.logo {
  text-align: center;
  height: 500rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  image {
    width: 355rpx;
    height: 179rpx;
  }
}
.butttonsubmit {
  width: 592rpx;
  height: 81rpx;
  background: #953b82;
  color: #ffffff;
  border-radius: 6rpx;
  margin-top: 200rpx;
}
</style>
